<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增首页轮播')"/>
    <link th:href="@{/upload/upload.css}" rel="stylesheet"/>
    <th:block th:include="include :: select2-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-WxBanner-add">
        <div class="form-group">
            <label class="col-sm-2 control-label is-required">跳转类型：</label>
            <div class="col-sm-10">
                <div class="radio-box" th:each="dict : ${@dict.getType('banner_link_type')}">
                    <input type="radio" th:id="${'bannerLinkType_' + dict.dictCode}" name="bannerLinkType"
                           th:value="${dict.dictValue}"
                           th:checked="${dict.default}">
                    <label th:for="${'bannerLinkType_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label is-required">图片
                ：</label>
            <div class="col-sm-10">
                <div class="upload" action='upload.html' data-num="1" data-value='' id="piclist_bannerPic"></div>
            </div>
        </div>
        <div class="form-group div_1">
            <label class="col-sm-2 control-label is-required">跳转目标
                ：</label>
            <div class="col-sm-10">
                <select name="bannerLinkId" id="bannerLinkId" class="form-control" type="text" required></select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label is-required">排序
                ：</label>
            <div class="col-sm-10">
                <input name="bannerSort" id="bannerSort" class="form-control" required
                       type="number">
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js"/>
<script type="text/javascript">
    var prefix = ctx + "wechat/WxBanner"


    $("#form-WxBanner-add").validate({
        focusCleanup: true
    });


    //单图上传
    $("#piclist_bannerPic").upload(function () {
    }, "bannerPic")

    init();
    $(document).ready(function () {
        $("input[type=radio][name='bannerLinkType']").on('ifChecked', function (event) {
            init()
        });
    });
    function init(){
        if ($("input[name='bannerLinkType']:checked").val() == '0') {
            initFarm();
            $(".div_1").css("display", "block");
        } else if($("input[name='bannerLinkType']:checked").val() == '1') {
            initWxCanteen();
            $(".div_1").css("display", "block");
        }else {
            $(".div_1").css("display", "none");
        }
    }
  function initFarm(){
      $("#bannerLinkId").empty();
      $.operate.ajaxGet(ctx + "/wechat/WxFarm/listAjax", function (result) {
          if(result.data.length==0){
              $.modal.alertError("暂无农场")
          }
          var html = "";
          for (var i = 0; i < result.data.length; i++) {
              html += "<option value='" + result.data[i].farmId + "'>" + result.data[i].farmName + "</option>";
          }
          $("#bannerLinkId").append(html);
      })
  }

  function  initWxCanteen(){
      $("#bannerLinkIs").empty();
      $.operate.ajaxGet(ctx + "/wechat/WxCanteen/listAjax", function (result) {
          if(result.data.length==0){
              $.modal.alertError("暂无餐厅")
          }
          var html = "";
          for (var i = 0; i < result.data.length; i++) {
              html += "<option value='" + result.data[i].canteenId + "'>" + result.data[i].canteenName + "</option>";
          }
          $("#bannerLinkIs").append(html);
      })
  }


    function submitHandler() {
        if ($.validate.form()) {
            if($("input[name='bannerPic']").val()==""){
                $.modal.msgError("请上传图片！")
                return
            }
            $.operate.save(prefix + "/add", $('#form-WxBanner-add').serialize());
        }
    }
</script>
</body>
</html>
